package com.hl.yyx.common.exception;

import com.hl.yyx.common.response.FailedResponse;
import com.hl.yyx.common.response.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.WebRequest;

import java.sql.SQLFeatureNotSupportedException;

/**
 * 全局异常处理
 * Created by macro on 2020/2/27.
 */
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ResponseBody
    @ExceptionHandler(value = ApiException.class)
    public Response handle(ApiException e) {
        if (e.getErrorCode() != null) {
            return new FailedResponse<>(e.getErrorCode() + "");
        }
        return new FailedResponse<>(e.getMessage());
    }

    @ExceptionHandler(value = InvalidDataAccessApiUsageException.class)
    public Response handleInvalidDataAccessApiUsage(InvalidDataAccessApiUsageException e, WebRequest request) {
        // 提取根本原因信息
        String rootCause = e.getRootCause() != null ?
                e.getRootCause().getMessage() : e.getMessage();
        request.getDescription(false).replace("uri=", "");
        if (e.getCause() instanceof SQLFeatureNotSupportedException) {
            String message = "数据库不支持请求的操作：" + rootCause;
            log.error(message);
            return new FailedResponse<>(message);
        }
        return new FailedResponse<>(e.getMessage());
    }

    @ExceptionHandler(value = SQLFeatureNotSupportedException.class)
    public Response handleSQLFeatureNotSupported(SQLFeatureNotSupportedException e) {
        return new FailedResponse<>(e.getMessage());
    }

    /**
     * 处理未知异常
     *
     * @param e
     * @return
     */
    @ResponseBody
    @ExceptionHandler(value = RuntimeException.class)
    public Response handleRuntimeException(RuntimeException e) {
        log.error("运行时异常：", e);
//        return CommonResult.failed(ResultCode.UN_KNOWN);
        // 返回具体异常
        return new FailedResponse<>(e.getMessage());

    }

    @ResponseBody
    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    public Response handleValidException(MethodArgumentNotValidException e) {
        BindingResult bindingResult = e.getBindingResult();
        String message = null;
        if (bindingResult.hasErrors()) {
            FieldError fieldError = bindingResult.getFieldError();
            if (fieldError != null) {
                message = fieldError.getDefaultMessage();
            }
        }
        return new FailedResponse<>(message);
    }

    @ResponseBody
    @ExceptionHandler(value = BindException.class)
    public Response handleValidException(BindException e) {
        BindingResult bindingResult = e.getBindingResult();
        String message = null;
        if (bindingResult.hasErrors()) {
            FieldError fieldError = bindingResult.getFieldError();
            if (fieldError != null) {
                message = fieldError.getField() + fieldError.getDefaultMessage();
            }
        }
        return new FailedResponse<>(message);
    }
}